package online.afeibaili.storebackendframework.mapper;

import online.afeibaili.storebackendframework.model.Coupon;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

//    private long createTime; // 创建时间
//    private double discount; // 优惠折扣
//    private double reprice; // 优惠价格
//    private double min; // 优惠最低价格
//    private int quantity; // 优惠卷数量
//    private String name; // 持有人是否使用
@Mapper
public interface CouponMapper {
    @Insert("INSERT INTO coupon(create_time, discount, reprice, min, quantity, name,expire_time) VALUES(#{createTime}, #{discount}, #{reprice}, #{min}, #{quantity}, #{name},#{expireTime})")
    int insert(Coupon coupon);
    @Select("SELECT * FROM coupon")
    @Result(property = "createTime", column = "create_time")
    @Result(property = "expireTime", column = "expire_time")
    List<Coupon> selectAll();
    @Select("SELECT count(*) FROM coupon WHERE create_time=#{createTime}")
    int isCount(long createTime);
    //根据创建时间更新name并且quantity-1当quantity<=0则失败
    @Update("UPDATE coupon SET name=#{name},quantity=quantity-1 WHERE create_time= #{createTime} AND quantity>0")
    int updateName(long createTime, String name);
    @Select("SELECT name,expire_time FROM coupon WHERE create_time= #{createTime}")
    @Result(property = "expireTime", column = "expire_time")
    Map<String,Object> selectName(long createTime);
    //获取优惠券总数
    @Select("SELECT count(*) FROM coupon")
    int getCount();
}
